const { getPool } = require('../../../config/db')

const HanziRadical = {
  async getAllHanziRadicals() {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('SELECT * FROM hanzi_radical')
    return rows
  },

  async getHanziRadicalById(id) {
    const mysqlPool = await getPool()
    const [rows] = await mysqlPool.execute('SELECT * FROM hanzi_radical WHERE id = ?', [id])
    return rows[0]
  },

  async createHanziRadical(radicalData) {
    const mysqlPool = await getPool()
    const [result] = await mysqlPool.execute(
      'INSERT INTO hanzi_radical (radical, name) VALUES (?, ?)',
      [radicalData.radical, radicalData.name]
    )
    return result.insertId
  },

  async updateHanziRadical(id, radicalData) {
    const mysqlPool = await getPool()
    await mysqlPool.execute(
      'UPDATE hanzi_radical SET radical=?, name=? WHERE id=?',
      [radicalData.radical, radicalData.name, id]
    )
  },

  async deleteHanziRadical(id) {
    const mysqlPool = await getPool()
    await mysqlPool.execute('DELETE FROM hanzi_radical WHERE id = ?', [id])
  },
}

module.exports = HanziRadical